package com.competition.web.admin.service.impl;

import java.util.List;

import org.springframework.core.ParameterizedTypeReference;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.competition.param.impl.OrganizeRequestParam;
import com.competition.web.admin.config.HttpRequest;
import com.competition.web.admin.service.ICacheService;
import com.competition.web.constant.RedisConstant;
import com.sync.tanm.SyncTanma;
import com.unify.type.Department;
import com.unify.type.DepartmentFlatten;
import com.unify.type.ExploringHorsesResponse;
import com.unify.type.SearchColumn;

@Service
public class CacheService implements ICacheService {

	private RedisTemplate<String, Object> redisTemplate;
	
	private HttpRequest httpRequest;
	
	SyncTanma syncTanma;
	
	public CacheService(RedisTemplate<String, Object> redisTemplate, HttpRequest httpRequest) {
		this.redisTemplate = redisTemplate;
		
		SyncTanma.init(httpRequest, redisTemplate);
		 
		syncTanma = SyncTanma.getInstance();
		
		this.httpRequest = httpRequest;
	}
	
	@Override
	public void resizeOrganize() {
		
		ParameterizedTypeReference<ExploringHorsesResponse<List<Department>>> parameterizedTypeReference = new ParameterizedTypeReference<ExploringHorsesResponse<List<Department>>>() {
		};

		ExploringHorsesResponse<List<Department>> exploringHorsesResponse = this.httpRequest
				.geOrganize(new OrganizeRequestParam(), parameterizedTypeReference);

		
		if (0 != exploringHorsesResponse.getCode()) {
			return;
		}
		
		List<Department> departments = exploringHorsesResponse.getData();
		
		if (null == departments) {
			return ;
		}

		ListOperations<String, Object> opsForList = redisTemplate.opsForList();
		
		String key = RedisConstant.getOrganizeKey();
		
		redisTemplate.delete(key);
		
		for (Department department : departments) {
			opsForList.rightPush(key, department);
		}
	}

	@Override
	public void resizeCityCondition() {
		ParameterizedTypeReference<ExploringHorsesResponse<List<SearchColumn>>> parameterizedTypeReference = new 
				ParameterizedTypeReference<ExploringHorsesResponse<List<SearchColumn>>>() {
		};
		
		redisTemplate.delete(RedisConstant.SEARCH_CITY_COLUMN);
		
		ExploringHorsesResponse<List<SearchColumn>> exploringHorsesResponse = this.httpRequest.postProfileFields(parameterizedTypeReference);
		

		List<SearchColumn> data = exploringHorsesResponse.getData();
		
		// 城市 搜索条件
		SearchColumn cityColumn = data.get(3);
		
		HashOperations<String, String, SearchColumn> opsForHash = this.redisTemplate.opsForHash();
		
		opsForHash.put(RedisConstant.SEARCH_CITY_COLUMN, RedisConstant.SEARCH_HASH_CITY, cityColumn);
	}
	

	@Override
	public void resizeClients() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void loadTanma() {
		// TODO Auto-generated method stub
		List<DepartmentFlatten> organizeData = syncTanma.getOrganizeData();

		HashOperations<String, String, Object> opsForHash = redisTemplate.opsForHash();

		this.httpRequest.getThreadPoolTaskExecutor().execute(() -> {
			syncTanma.pushCompanyInfo(organizeData, opsForHash);
		});
	}

}
